package org.crystalrange.documents.web;

import java.util.List;

import org.crystalrange.documents.model.DocumentGroup;
import org.crystalrange.documents.service.DocumentGroupService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.databind.AnnotateDataBinder;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import com.googlecode.genericdao.search.Search;

public class DocumentGroupSelectController extends GenericForwardComposer {

	protected DocumentGroupService documentGroupService;
	protected DocumentGroup selectedDocumentGroup;
	protected List<DocumentGroup> documentGroups;
	protected Search searchDocumentGroup;
	protected AnnotateDataBinder binder;

	public DocumentGroupService getDocumentGroupService() {
		return documentGroupService;
	}

	public void setDocumentGroupService(
			DocumentGroupService documentGroupService) {
		this.documentGroupService = documentGroupService;
	}

	public DocumentGroup getSelectedDocumentGroup() {
		return selectedDocumentGroup;
	}

	public void setSelectedDocumentGroup(DocumentGroup selectedDocumentGroup) {
		this.selectedDocumentGroup = selectedDocumentGroup;
	}

	public List<DocumentGroup> getDocumentGroups() {
		return documentGroups;
	}

	public void setDocumentGroups(List<DocumentGroup> documentGroups) {
		this.documentGroups = documentGroups;
	}

	@Override
	public void doAfterCompose(Component component) throws Exception {
		super.doAfterCompose(component);
		this.searchDocumentGroup = new Search(DocumentGroup.class);
		this.searchDocumentGroup.setMaxResults(100);
		this.searchDocumentGroup.addFetch("createdBy");
		this.documentGroups = this.documentGroupService
				.search(this.searchDocumentGroup);
		component.setAttribute("controller", this);
	}

	public void onClick$buttonOk(Event event) throws Exception {
		if (this.selectedDocumentGroup != null) {
			this.execution.setAttribute("selectedDocumentGroup",
					this.selectedDocumentGroup);
			this.execution.setAttribute("result", "ok");
			this.self.detach();
		} else {
			Messagebox.show("Please select a Document Group.");
		}
	}

	public void onClick$buttonCancel(Event event) throws Exception {
		this.execution.setAttribute("selectedDocumentGroup", null);
		this.execution.setAttribute("result", "cancel");
		this.self.detach();
	}

	public void onClick$buttonSearch(Event event) throws Exception {
		this.execution.setAttribute("searchDocumentGroup",
				this.searchDocumentGroup);

		Window window = (Window) this.execution.createComponents(
				"~./zul/html/documentGroupSearch.zul", null, null);
		window.setParent(self);
		window.doModal();

		if (this.execution.getAttribute("result") == "ok") {
			this.searchDocumentGroup = (Search) this.execution
					.getAttribute("searchDocumentGroup");
		}

		this.documentGroups = this.documentGroupService
				.search(this.searchDocumentGroup);
		this.binder.loadAll();
	}
}
